/*
 * UserManager.cpp
 *
 *  Created on: 13/10/2011
 *      Author: mario
 */

#include "UserManager.h"
#include "../../util/NodoVotante.h"
#include "../../util/NodoEleccion.h"
#include "../../util/Utils.h"

#include "../log/Logger.h"

#include "../RSA/RSAEncryption.h"

UserManager::UserManager() {

}

UserManager::~UserManager() {
}



List *UserManager::getUsers() const
{
   return users;
}

int UserManager::saveUser(Votante *votante)
{
	int result;
	tree::BPlusTree * tree= new tree::BPlusTree(VOTANTES,BLOCKSIZE);
	stringstream ss(ios_base::in | ios_base::out | ios_base::binary);

	votante->toData(&ss);
	tree::Record* record = new tree::Record();

	record->edit(votante->getDni(), &ss);

	result = tree->modify(record);
	return result;
}

int UserManager::addUser(Votante *votante)
{
	int result;
	tree::BPlusTree * tree= new tree::BPlusTree(VOTANTES,BLOCKSIZE);
	stringstream ss(ios_base::in | ios_base::out | ios_base::binary);

	votante->toData(&ss);
	tree::Record* record = new tree::Record();

	record->edit(votante->getDni(), &ss);

	result = tree->insert(record);
	return result;
}

int UserManager::deleteUser(Votante *votante)
{
	int result;
	tree::BPlusTree * tree= new tree::BPlusTree(VOTANTES,BLOCKSIZE);
	stringstream ss(ios_base::in | ios_base::out | ios_base::binary);

	votante->toData(&ss);
	tree::Record* record = new tree::Record(votante->getDni());
	result = tree->remove(record);
	return result;
}

void UserManager::setUsers(List *users)
{
    this->users = users;
}

Votante *UserManager::searchUser(int userDni, string password)
{
	tree::BPlusTree * tree=new tree::BPlusTree(VOTANTES,BLOCKSIZE);
	tree::Record * rec= new tree::Record();
	rec->setKey(userDni);
	tree::Record * recVot=tree->find(rec);
	if(recVot==NULL){
		Logger::createEntryLog("VOTANTE INVALIDO, NO SE ENCUENTRA REGISTRADO",Logger::LOG);
		return NULL;
	}else{
		stringstream sss(ios_base::in| ios_base::out| ios_base::binary);
		sss.write(recVot->getData(), recVot->getDataSize());

		Votante * usr = Votante::toObject(&sss);

		RSAEncryption* rsa = RSAEncryption::instance();
		string cleanPassword = rsa->decrypt(password);

		if(usr->getPassword().compare(cleanPassword) == 0){
			return usr;
		}
		Logger::createEntryLog("LA CLAVE INGRESADA NO COINCIDE CON EL VOTANTE INGRESADO",Logger::LOG);
		return NULL;
	}
}

Votante *UserManager::searchUser(int userDni)
{
	tree::BPlusTree * tree=new tree::BPlusTree(VOTANTES,BLOCKSIZE);
	tree::Record * rec= new tree::Record();
	rec->setKey(userDni);
	tree::Record * recVot=tree->find(rec);
	if(recVot==NULL){
		Logger::createEntryLog("VOTANTE INVALIDO",Logger::LOG);
		return NULL;
	}else{
		stringstream sss(ios_base::in| ios_base::out| ios_base::binary);
		sss.write(recVot->getData(), recVot->getDataSize());
		return Votante::toObject(&sss);
	}
}

List *UserManager::returnAll()
{
	List* ret = new List();
	tree::BPlusTree* tree = new tree::BPlusTree(VOTANTES, BLOCKSIZE);
	tree::Record* record = tree->iterate();
	while(record != NULL){
		stringstream ss(ios_base::in| ios_base::out| ios_base::binary);
		ss.write(record->getData(), record->getDataSize());
		Votante* votante = Votante::toObject(&ss);
		NodoVotante* nodo = new NodoVotante();
		nodo->setNodeValue(votante);
		ret->add(nodo);
		record = tree->iterate();
	}
	return ret;
}








